import axios from "axios";

const service = axios.create({
    baseURL: "", // API的base_url
    timeout: 5000 // 请求超时时间
});

service.interceptors.request.use(
    config => {
        const token = localStorage.getItem("token"),
            userId = localStorage.getItem("userId");
        config.headers["Authorization"] = `Bearer ${token}`;
        config.headers["userId"] = userId;
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

service.interceptors.response.use(
    response => {
        if (response.status == 200 && response.data.code == 200) {
            return Promise.resolve(response.data);
        } else {
            return Promise.reject(response.data);
        }
    },
    error => {
        return Promise.reject(error);
    }
);

export default service;
